﻿
using MimeKit;
using MimeKit.Text;
using System.Net.Mail;

namespace Email.Services
{
    public class EmailHelper
    {

        const string mailFrom = "发送者名称";
        const string mailTo = "接收者账号";
        const string mailFromAccount = "发送者账号";
        const string mailPassword = "发送者账号密码";
        public void SendEmail()
        {
            Console.WriteLine("开始发送邮件...");
            var message = new MimeMessage();
            message.From.Add(new MailboxAddress("信息系统管理员", mailFrom));
            message.To.Add(new MailboxAddress("", mailTo));
            message.Subject = string.Format("C#自动发送邮件测试 From geffzhang TO {0}", mailTo);

            var plain = new TextPart("plain")
            {
                Text = @"20220101！"
            };
            var html = new TextPart("html")
            {
                Text = @"您本次登录需要验证码; <br/> 请输入验证码 123654 ，本次验证码 10 分钟内有效！"
            };
            var alternative = new Multipart("alternative");
            alternative.Add(plain);
            alternative.Add(html);
             
            var multipart = new Multipart("mixed");
            multipart.Add(alternative);
            message.Body = multipart;

            try
            {
                using (var client = new MailKit.Net.Smtp.SmtpClient())
                {
                    //HTTPS邮件服务器用587端口，HTTP默认25端口
                    client.Connect("smtp.exmail.qq.com", 587, false);
                    // 禁用XOAUTH2认证
                    client.AuthenticationMechanisms.Remove("XOAUTH2");
                    // 注意：仅在SMTP服务器需要身份验证时才需要
                    client.Authenticate(mailFromAccount, mailPassword);
                    client.Send(message);
                    client.Disconnect(true);
                }

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        public void TestSmtpClient()
        {
            MailMessage mymail = new MailMessage();
            mymail.From = new System.Net.Mail.MailAddress(mailFrom);
            mymail.To.Add(mailTo);
            mymail.Subject = string.Format("C#自动发送邮件{0}", mailTo);
            mymail.Body = @"<p>自动邮件测试<br>";
            mymail.IsBodyHtml = true;
            //mymail.Attachments.Add(new Attachment(path));

            System.Net.Mail.SmtpClient smtpclient = new System.Net.Mail.SmtpClient();
            smtpclient.Port = 587;
            smtpclient.UseDefaultCredentials = false;
            smtpclient.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtpclient.Host = "smtp.exmail.qq.com";
            smtpclient.EnableSsl = true;
            smtpclient.Credentials = new System.Net.NetworkCredential(mailFromAccount, mailPassword);
            try
            {
                smtpclient.Send(mymail);
                Console.WriteLine("发送成功");
            }
            catch (Exception ex)
            {
                Console.WriteLine("发送邮件失败" + ex);

            }
        }


    }

}
